﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
using System.Xml.Serialization;

namespace Powershell_ML
{
    public class MailboxDatabase : Exchange_Information
    {
        public MailboxDatabase()
        {
        }
        public void RefreshStats()
        {
            if (this.has_stats)
            {
                this.Stats = null;
                this.Last_Stats_Load = DateTime.Now;
                Console.WriteLine("Last Stats Load was : {0}", this.Last_Stats_Load);
                this.Stats = new List<MailboxDatabase>();
                this.Stats = this.Export_Stats("", this.Name);

            }

        }
        public MailboxDatabase(PSObject Object)
        {
            //Constructor Logic
            Powershell_ML.Famework.IEntry entry = new Powershell_ML.Famework.Powershell_Utility();
            //GenericPropertyList
            this.Property = new List<PSPropertyInfo>();
            this.Property = entry.Property_Creator(Object);
            this.Collected_Value = Property_Collection(Object);
            this.ID = Guid.NewGuid();

            //Comon Used Property


            this.Name = this.Extract_String_Property(this.Property, "Name");
            this.Server = this.Extract_String_Property(this.Property, "Server");
            this.ReplicationType = this.Extract_String_Property(this.Property, "ReplicationType");
            this.EdbFilePath = this.Extract_String_Property(this.Property, "EdbFilePath");
            this.PublicFolderDatabase = this.Extract_String_Property(this.Property, "PublicFolderDatabase");
            this.LogFolderPath = this.Extract_String_Property(this.Property, "LogFolderPath");
            this.ProhibitSendReceiveQuota = Convert.ToString(entry.Size_Formater(this.Extract_String_Property(this.Property, "ProhibitSendReceiveQuota")));
            this.ProhibitSendQuota = Convert.ToString(entry.Size_Formater(this.Extract_String_Property(this.Property, "ProhibitSendQuota")));

            //Statistics Property
            this.Collected_Date = DateTime.Now;
            if (this.Has_MDB_Stats("", this.Name))
            {
                this.Stats = new List<MailboxDatabase>();
                this.has_stats = true;
                this.Last_Stats_Load = DateTime.Now;
                this.Stats = this.Export_Stats("", this.Name);
                this.MDB_Size_History = new Dictionary<DateTime, int>();
                this.MDB_Mbx_History = new Dictionary<DateTime, int>();
                this.MDB_Srv_History = new Dictionary<DateTime, string>();
                this.Stats.ForEach(delegate (MailboxDatabase MDB)
                {
                    var convertDecimal = Convert.ToDecimal(MDB.Size);
                    this.MDB_Size_History.Add(MDB.Collected_Date, Convert.ToInt32(convertDecimal));
                    this.MDB_Mbx_History.Add(MDB.Collected_Date, Convert.ToInt32(MDB.Mailbox_Count));
                    this.MDB_Srv_History.Add(MDB.Collected_Date, MDB.Server);
                });
                IEnumerable<int> value = this.MDB_Size_History.Values;
                IEnumerable<DateTime> Date = this.MDB_Size_History.Keys;
                DateTime Start_Range = Date.Min();
                DateTime End_Range = Date.Max();
                TimeSpan Duration = End_Range - Start_Range;

                int Min = value.Min();
                int Max = value.Max();
                int PeriodVol = Max - Min;

                if (PeriodVol > 0 & Min >0 & Max > 0)
                {
                    double percentcomplet = Math.Round((double)(100 * PeriodVol) / Max);
                    double percentagegrowth = (Max - Min) / Min;
                    //Console.WriteLine("Statistic Period : " + Format_TimeSpan_Duration(Duration));
                    //Console.WriteLine(this.Name);
                    //Console.WriteLine("Mailbox Database Taken : " + Format_MB(PeriodVol.ToString()));
                    //Console.WriteLine("Mailbox Database Grow Of : " + percentagegrowth.ToString());
                    //Console.WriteLine(percentcomplet.ToString("0.00%"));
                    //Console.WriteLine("Between the : " + Start_Range.ToString());
                    //Console.WriteLine("And the : " + End_Range.ToString());
                }
            }

            if (Object.Properties["DatabaseSize"].Value != null)
            {
                object container = Object.Properties["DatabaseSize"].Value;
                this.Size = entry.Size_Formater(container.ToString()).ToString();
            }
            this.Mailbox_Count = MailboxCount_ByDatabase(this.Name);

        }
        [XmlIgnoreAttribute]
        public DateTime Last_Stats_Load
        {
            get;set;
        }
        [XmlIgnoreAttribute]
        public Dictionary<DateTime, string> MDB_Srv_History { get; set; }
        [XmlIgnoreAttribute]
        public Dictionary<DateTime, int> MDB_Mbx_History { get; set; }
        [XmlIgnoreAttribute]
        public Dictionary<DateTime, int> MDB_Size_History {get;set;}
        public Guid ID
        {
            get;set;
        }
        [XmlIgnoreAttribute]
        public List<MailboxDatabase> Stats
        {
            get;set;
        }
        [XmlIgnoreAttribute]
        public bool has_stats
        {
            get;set;

        }
        public int Mailbox_Count { get; set; }
        public string Size { get; set; }
        public string Name { get; set; }
        public string Server { get; set; }
        public string ReplicationType { get; set; }
        public string EdbFilePath { get; set; }
        public string PublicFolderDatabase { get; set; }
        public string ProhibitSendReceiveQuota { get; set; }
        public string ProhibitSendQuota { get; set; }
        public string LogFolderPath { get; set; }
        public DateTime Collected_Date { get; set; }
        [XmlIgnoreAttribute]
        public Dictionary<string, string> Wanted
        {
            get;
            set;
        }
        [XmlIgnoreAttribute]
        public List<ICollection> Collected_Value
        {
            get;
            set;
        }
        [XmlIgnoreAttribute]
        public List<PSPropertyInfo> Property
        {
            get;
            set;
        }
    }
}
